/**

 @Name：layuiAdmin 菜单资源

 */


layui.define(['table', 'form'], function (exports) {
    var $ = layui.$
        , admin = layui.admin
        , view = layui.view
        , table = layui.table
        , form = layui.form;

    // 设置token
    table.set({headers: {Authorization: 'sdfsdfsdfds'}});
    //用户管理
    table.render({
        elem: '#LAY-resource-tb'
        , url: '/api/resources/list' //模拟接口
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 100, title: 'ID', sort: true, hide: false}
            , {field: 'name', title: '资源名称', minWidth: 100}
            , {field: 'code', minWidth: 120, title: '资源编码'}
            , {
                field: 'type', width: 120, title: '资源类型', align: 'center', templet: function (d) {
                    if (d.type == 1) {
                        return '<div><span class="layui-badge layui-bg-blue">菜单</span></div>'
                    } else {
                        return '<div><span class="layui-badge layui-bg-green">按钮</span></div>'
                    }
                }
            }
            , {field: 'jump', title: '资源路径'}
            , {field: 'sort', title: '排序', sort: true}
            , {field: 'icon', title: '图标'}
            , {field: 'available', title: '资源状态', templet:function (d) {
                 if (d.available == 0) {
                     return '<div><span class="layui-badge layui-bg-blue">可用</span></div>'
                 } else {
                     return '<div><span class="layui-badge layui-bg-gray">不可用</span></div>'
                 }
                }}
            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-resource-list'}
        ]]
        , response: {
            statusName: 'code' //规定数据状态的字段名称，默认：code
            , statusCode: 20000 //规定成功的状态码，默认：0
            , msgName: 'message' //规定状态信息的字段名称，默认：msg
            , countName: 'count' //规定数据总数的字段名称，默认：count
            , dataName: 'data' //规定数据列表的字段名称，默认：data
        }
        , page: true
        , limit: 10
        , height: 'full-320'
        , text: '对不起，加载出现异常！'
        , done: function (res, curr, count) {
            if (res.count == 0) {
                $(".layui-table-main").html('<div class="layui-none">暂无数据</div>');
            }
        }
    });

    //监听工具条
    table.on('tool(LAY-resource-tb)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                obj.del();
                layer.close(index);
            });
        } else if (obj.event === 'edit') {
            admin.popup({
                title: '编辑资源'
                , area: ['600px', '650px']
                , id: 'LAY-popup-resource-add'
                , success: function (layero, index) {
                    view(this.id).render('user/resource/form', data).done(function () {
                        form.render(null, 'layuiadmin-form-resource');

                        //监听提交
                        form.on('submit(LAY-resource-front-submit)', function (data) {
                            var field = data.field; //获取提交的字段

                            //提交 Ajax 成功后，关闭当前弹层并重载表格
                            layui.table.reload('LAY-resource-tb'); //重载表格
                            layer.close(index); //执行关闭
                        });
                    });
                }
            });
        }
    });

    exports('resource/list', {})
});